package com.example.demo.service.impl;

import com.example.demo.dao.UserDepartMapper;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDepartMapper userDepartMapper;


    @Override
    public List<User> listUsers(User userParam, int pageNum, int pageSize) {
        // 查询并设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        //查询user表
        String departIdStr = null;
        if (userParam.getDepartIds() != null) {
            departIdStr = userParam.getDepartIds().stream().map(Objects::toString).collect(Collectors.joining(","));
        }
        List<User> userList = userMapper.selectUsers(userParam, departIdStr);
        //查询userDepart表
        for (User user : userList) {
            List<Long> departIds = userDepartMapper.seleteUserDepartByUserId(user.getUserId());
            user.setDepartIds(departIds);
        }

        return userList;
    }

    @Override
    public boolean deleteUser(long userId) {
        //逻辑删除，状态置为0
        if (userMapper.deleteUser(userId) == 0) {
            return false;
        }
        return true;
    }

    @Override
    public String saveUser(User user) {
        int result = userMapper.addUser(user);
        if (result == 0) {
            return "insert user error";
        }
        if (user.getDepartIds() != null && user.getDepartIds().size() > 0) {
            for (long departId : user.getDepartIds()) {
                result = userDepartMapper.saveUserDepart(user.getUserId(), departId);
                if (result == 0) {
                    return "insert user department error";
                }
            }
        }
        return "success";
    }

    @Override
    public String updateUser(User userParams) {
        // 更新基本信息 int result = userMapper.updateUser(userParams);
        // 更新部门
        long userId = userParams.getUserId();
        // 更新基本信息
        boolean result = true;
        if (userParams.getName() != null || userParams.getSex() != null ||
                userParams.getAge() != null || userParams.getOccupation() != null) {
            result &= userMapper.updateUser(userParams) == 1;
        }
        // 更新部门
        List<Long> paramsDepartIds = userParams.getDepartIds();
        if (paramsDepartIds != null) {
            List<Long> dbDepartIds = userDepartMapper.seleteUserDepartByUserId(userId);
            for (Long dbDepartId : dbDepartIds) {
                // db中的部门在参数中不存在的：删除
                if (!userParams.getDepartIds().contains(dbDepartId)) {
                    result &= userDepartMapper.deleteUserDepart(userId, dbDepartId) == 1;
                }

            }
            if (!result) {
                return "Delete departments of users failed!";
            }
            for (Long paramDepartId : userParams.getDepartIds()) {
                // 参数中的部门在数据库不存在的：新增
                if (!dbDepartIds.contains(paramDepartId)) {
                    result &= userDepartMapper.saveUserDepart(userId, paramDepartId) == 1;
                }
                if (!result) {
                    return "Add departments of users failed!";
                }
            }
        }
        return "success";
    }


}


